package com.whlilong.jxc.ui.sorter;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import com.whlilong.jxc.model.Kstype;

public class  MerchantTypeSorter extends ViewerSorter{

		// 每列对应一个不同的常量，正数表示要升序、相反数表示要降序

		private static final int ID = 1;

		private static final int kstpid = 2;

		private static final int kstpname = 3;

		// 给外界使用排序器对象

		public static final MerchantTypeSorter ID_ASC = new MerchantTypeSorter(ID);

		public static final MerchantTypeSorter ID_DESC = new MerchantTypeSorter(-ID);

		public static final MerchantTypeSorter kstpid_asc = new MerchantTypeSorter(kstpid);

		public static final MerchantTypeSorter kstpid_desc = new MerchantTypeSorter(-kstpid);

		public static final MerchantTypeSorter kstpname_asc = new MerchantTypeSorter(kstpname);

		public static final MerchantTypeSorter kstpname_desc = new MerchantTypeSorter(-kstpname);

		// 当前所要排序的列，取自上面的ID、NAME两值或其相反数

		private int sortType;

		// 构造函数用private，表示不能在外部创建MySorter对象

		private MerchantTypeSorter(int sortType) {

			this.sortType = sortType;

		}

		// 最关键的比较方法compare，改写自ViewerSorter。方法返回值是一个int值：正数则

		// obj1移到obj2之前；零则obj1和obj2的位置不动；负数则obj1移到obj2之后

		public int compare(Viewer viewer, Object obj1, Object obj2) {

			// 传入两条记录（实体类），然后依列给出它们的先后顺序

			Kstype o1 = (Kstype) obj1;

			Kstype o2 = (Kstype) obj2;

			switch (sortType) {
				case ID: {
					int l1 = o1.getId();
					int l2 = o2.getId();
					// Long的compareTo方法返回值有三个可能值1,0,-1：
		
					// 如l1＞l2则返回1；如l1＝l2则返回0；如l1＜l2则返回-1
					return l1 > l2 ? 1 : -1;
				}
		
				case -ID: {
					int l1 = o1.getId();
					int l2 = o2.getId();
					return l2 > l1 ? 1 : -1;
				}
				case kstpid: {
					String s1 = o1.getKstpid();
					String s2 = o2.getKstpid();
					return s1.compareTo(s2);
				}
		
				case -kstpid: {
					String s1 = o1.getKstpid();
					String s2 = o2.getKstpid();
					return s2.compareTo(s1);
				}
				case kstpname: {
					String s1 = o1.getKstype();
					String s2 = o2.getKstype();
					return s1.compareTo(s2);
				}
				case -kstpname: {
					String s1 = o1.getKstype();
					String s2 = o2.getKstype();
					return s2.compareTo(s1);
				}
			}
			return 0;

		}


}
